package com.smileymedia.util;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.smileymedia.core.Constants;

import android.util.Log;

/**
 * Android Utility Helper class for dealing with url requests
 * @author greg
 *
 */
public class URLUtil {
	private static final String TAG = URLUtil.class.getSimpleName();

	/**
	 * Read a url using GET Method
	 * @param sUrl
	 * @return
	 */
	public static String executeGet(String sUrl) 
	{ 
		StringBuilder builder=new StringBuilder();
		try {			
			URL url=new URL(sUrl);

			URLConnection uc=url.openConnection();
			uc.setUseCaches(true);
			InputStream stream=uc.getInputStream();
			BufferedReader in = new BufferedReader(new InputStreamReader(stream), 1024);
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				builder.append(inputLine);
			}
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 

		return builder.toString();
	} 

	/**
	 * 
	 * @param sUrl
	 * @param fields
	 * @return
	 */
	private static String executePOST(String sUrl, Map<String, String> fields) 
	{ 
		String encoding="UTF-8";
		String data="";
		//Construct data and encode it
		try {			
			Set<Entry<String,String>> entries=fields.entrySet();
			Iterator<Entry<String,String>> iter=entries.iterator();
			while(iter.hasNext()){
				Entry<String, String> entry=iter.next();
				data+=URLEncoder.encode(entry.getKey(), encoding)+"="+URLEncoder.encode(entry.getValue(), encoding)+"&";
			}

		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		//POST and Read back response
		StringBuilder builder=new StringBuilder();
		try {			
			//Setup
			URL url=new URL(sUrl);
			URLConnection uc=url.openConnection();
			uc.setUseCaches(true);
			uc.setDoOutput(true);
			//Post data
			OutputStreamWriter wr=new OutputStreamWriter(uc.getOutputStream());
			wr.write(data);
			wr.flush();

			//Read response
			InputStream stream=uc.getInputStream();
			BufferedReader in = new BufferedReader(new InputStreamReader(stream), 1024);
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				builder.append(inputLine);
			}
			in.close();
			wr.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 

		return builder.toString();
	} 

	/**
	 * Read url and return content in bytes
	 * @param sUrl
	 * @return
	 */
	public static byte[] readURLtoByte(final String sUrl){
		if(Constants.IS_NULL_DATA_TEST){
			return null;
		}
		InputStream streamA = null;
		try {			
			URL url=new URL(sUrl);
			streamA=url.openStream();	

			return readStreamToByte(streamA);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				streamA.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


	/**
	 * Read in stream of data into a byte array
	 * @param stream
	 * @return
	 */
	public static byte[] readStreamToByte(InputStream stream){
		if(Constants.IS_NULL_DATA_TEST){
			return null;
		}

		BufferedInputStream bin = new BufferedInputStream(stream);	
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		try {
			while (bin.available() > 0) {
				baos.write(bin.read());				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return  baos.toByteArray();
	}
}
